package com.tc.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.Result;
import com.baidu.unbiz.fluentvalidator.Validator;
import com.baidu.unbiz.fluentvalidator.jsr303.HibernateSupportedValidator;
import com.tc.qichacha.stock.Stock;
import com.tc.qichacha_new.stock.EquityShareDetail;
import com.tc.utils.image.tree.RectNode;
import org.hibernate.validator.HibernateValidator;
import org.springframework.util.StringUtils;

import javax.validation.Validation;
import javax.validation.ValidatorFactory;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Set;

import static com.baidu.unbiz.fluentvalidator.ResultCollectors.toSimple;

public class ToolsHt {

    public static String respond(String fileName){
        System.out.println(fileName);
        try {
            String content=new String(Files.readAllBytes(Paths.get(fileName)),Charset.forName("UTF-8"));//specify the charset
            return content;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void eachKeySet(Object o){
        JSONObject jsonObject=null;
        if(o instanceof JSONArray){
            jsonObject=(JSONObject)((JSONArray) o).get(0);
        }else if(o instanceof JSONObject){
            jsonObject=(JSONObject)o;
        }else {
            System.out.println("无法解析");
            return;
        }
        Set<String> strings = jsonObject.keySet();
        for (String string:strings) {
            Object o1=jsonObject.get(string);
            if(o1 instanceof String)
                System.out.println("private String " +toLowerCaseFirstOne(string)+";");
            else if(o1 instanceof JSONObject)
                System.out.println("private "+ string +" "+toLowerCaseFirstOne(string)+";");
            else if(o1 instanceof JSONArray)
                System.out.println("private List<"+ string +"> "+toLowerCaseFirstOne(string)+";");
            else
                System.out.println("private xxx " +toLowerCaseFirstOne(string)+";");
        }
    }
    public static String toLowerCaseFirstOne(String s){
        if(Character.isLowerCase(s.charAt(0)))
            return s;
        else
            return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString();
    }

    public static RectNode eachChildren(Stock stock){
        List<Stock> childrens = stock.getChildren();
        RectNode rectNode=new RectNode(generateName(stock));
        if(childrens==null||childrens.isEmpty())
            return rectNode;
        for (Stock children:childrens) {
            if(children!=null){
                rectNode.addChild(eachChildren(children));
            }
        }
        return rectNode;
    }

    public static RectNode eachChildren_(EquityShareDetail equityShareDetail1){
        List<EquityShareDetail> detailList1 = equityShareDetail1.getDetailList();
        RectNode rectNode=new RectNode(generateName(equityShareDetail1));
        if(detailList1==null||detailList1.isEmpty())
            return rectNode;
        for (EquityShareDetail equityShareDetail:detailList1) {
            if(equityShareDetail!=null){
                rectNode.addChild(eachChildren_(equityShareDetail));
            }
        }
        return rectNode;
    }

    public static String generateName(EquityShareDetail equityShareDetail1){
        if(equityShareDetail1==null) return null;
        String s=equityShareDetail1.getName();
        if(!StringUtils.isEmpty(equityShareDetail1.getPercent())){
            s += " 股权比例:"+equityShareDetail1.getPercent();
        }
        if(!StringUtils.isEmpty(equityShareDetail1.getShouldCapi())){
            s += " 认缴金额:"+equityShareDetail1.getShouldCapi();
        }
        return s;
    }

    public static String generateName(Stock stock){
        if(stock==null) return null;
        String s=stock.getName();
        if(!StringUtils.isEmpty(stock.getFundedRate())){
            s += " 股权比例:"+stock.getFundedRate();
        }
        if(!StringUtils.isEmpty(stock.getFundedAmount())){
            s += " 认缴金额:"+stock.getFundedAmount();
        }
        return s;
    }

    public static ValidatorFactory validatorFactory = Validation.byProvider(HibernateValidator.class ).configure().buildValidatorFactory();
    public static  HibernateSupportedValidator hibernateSupportedValidator = new HibernateSupportedValidator().setHiberanteValidator(validatorFactory.getValidator());
    public static Result validateByCustomAndJSR303(Object o, Validator v){
        return FluentValidator.checkAll().on(o, v).on(o,hibernateSupportedValidator).doValidate().result(toSimple());
    }
    public static Result validateByCustom(Object o,Validator v){
        return FluentValidator.checkAll().on(o, v).doValidate().result(toSimple());
    }
    public static Result validateByJSR303(Object o){
        return FluentValidator.checkAll().on(o,hibernateSupportedValidator).doValidate().result(toSimple());
    }
}
